package cz.kz.cmdapp;

import cz.kz.model.KZEntry;
import cz.kz.model.KZUser;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.HashMap;
import java.util.List;

/**
 * Libraries needed in the classpath: javaee.jar, derby.jar and toplink/hibernate (toplink-essentials-*.jar)
 *
 * User: lzap
 * Date: 24.10.2007
 * Time: 22:58:57
 */
public class CreateTestingData {

    public static void main(String[] args) {

        HashMap<String, String> properties = new HashMap<String, String>();
        properties.put("toplink.ddl-generation", "drop-and-create-tables");
        properties.put("toplink.jdbc.url", "jdbc:derby:database;create=true");

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("kzPersistenceUnit", properties);
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();

        tx.begin();

        // nove instance
        System.out.println("Creating data...");
        KZUser lzap = new KZUser();
        lzap.setLogin("lzap");
        lzap.setPassword("lzap");
        em.persist(lzap);

        KZUser wlk = new KZUser();
        wlk.setLogin("wlk");
        wlk.setPassword("wlk");
        em.persist(wlk);

        KZUser karkulka = new KZUser();
        karkulka.setLogin("karkulka");
        karkulka.setPassword("karkulka");
        em.persist(karkulka);

        KZEntry vitejte = new KZEntry();
        vitejte.setTitle("Vítejte v JPA");
        vitejte.setText("Java Persistence API");
        em.persist(vitejte);
        lzap.addEntry(vitejte);

        KZEntry pivo = new KZEntry();
        pivo.setTitle("Pivo");
        pivo.setText("Pivo pivečko, to já mám rád");
        em.persist(pivo);
        wlk.addEntry(pivo);

        KZEntry vino = new KZEntry();
        vino.setTitle("Víno");
        vino.setText("To jsou věci po dvou deci :-)");
        em.persist(vino);
        wlk.addEntry(vino);

        // ulozime to do databaze
        em.merge(lzap);
        em.merge(wlk);
        em.merge(karkulka);

        em.merge(vitejte);
        em.merge(pivo);
        em.merge(vino);

        tx.commit();

        System.out.println("Data created...");
        em.close();

    }
}
